/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.dao.impl;

import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.basics.manager.ExceptionManager;
import com.genia.toolbox.persistence.bean.Persistable;
import com.genia.toolbox.persistence.dao.AbstractDAO;
import com.genia.toolbox.persistence.dao.SpecificDAO;
import com.genia.toolbox.persistence.exception.ObjectIsNotPersistableException;
import com.genia.toolbox.persistence.exception.PersistenceException;

/**
 * implementation of {@link SpecificDAO}.
 * 
 * @param <TYPE>
 *          the type of the publicly seen object
 */
public class AbstractSpecificDAO<TYPE>
    extends AbstractDAO
    implements SpecificDAO<TYPE>
{

  /**
   * the {@link ExceptionManager} to use.
   */
  private ExceptionManager exceptionManager;

  /**
   * the class of the real type of the objects handled by this DAO.
   */
  private transient final Class<? extends TYPE> persistentTypeClass;



  /**
   * constructor.
   * 
   * @param persistentTypeClass
   *          the class of the real type of the objects handled by this DAO
   */
  protected AbstractSpecificDAO(Class<? extends TYPE> persistentTypeClass)
  {
    this.persistentTypeClass = persistentTypeClass;
  }



  /**
   * delete a persistent object handled by this DAO.
   * 
   * @param object
   *          the object to delete
   * @throws PersistenceException
   *           if an error occured
   * @see com.genia.toolbox.persistence.dao.SpecificDAO#delete(java.lang.Object)
   */
  public void delete(TYPE object)
      throws PersistenceException
  {
    if (!(object instanceof Persistable<?>)) {
      throw new ObjectIsNotPersistableException();
    }
    super.delete((Persistable<?>) object);
  }



  /**
   * returns a new instance of the object handled by this DAO.
   * 
   * @return a new instance of the object handled by this DAO
   * @throws TechnicalException
   *           if an error occured
   * @see com.genia.toolbox.persistence.dao.SpecificDAO#newObject()
   */
  public TYPE newObject()
      throws TechnicalException
  {
    try {
      return persistentTypeClass.newInstance();
    }
    catch (Exception e) {
      throw getExceptionManager().convertException(e);
    }
  }



  /**
   * save a not yet persistent object handled by this DAO.
   * 
   * @param object
   *          the object to save
   * @throws PersistenceException
   *           if an error occured
   * @see com.genia.toolbox.persistence.dao.SpecificDAO#save(java.lang.Object)
   */
  public void save(TYPE object)
      throws PersistenceException
  {
    if (!(object instanceof Persistable<?>)) {
      throw new ObjectIsNotPersistableException();
    }
    super.save((Persistable<?>) object);
  }



  /**
   * update a persistent object handled by this DAO.
   * 
   * @param object
   *          the object to update
   * @throws PersistenceException
   *           if an error occured
   * @see com.genia.toolbox.persistence.dao.SpecificDAO#update(java.lang.Object)
   */
  public void update(TYPE object)
      throws PersistenceException
  {
    if (!(object instanceof Persistable<?>)) {
      throw new ObjectIsNotPersistableException();
    }
    super.update((Persistable<?>) object);
  }



  /**
   * getter for the exceptionManager property.
   * 
   * @return the exceptionManager
   */
  public ExceptionManager getExceptionManager()
  {
    return exceptionManager;
  }



  /**
   * setter for the exceptionManager property.
   * 
   * @param exceptionManager
   *          the exceptionManager to set
   */
  public void setExceptionManager(ExceptionManager exceptionManager)
  {
    this.exceptionManager = exceptionManager;
  }

}
